package com.ebo.corelib.ui.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;

import devlight.io.library.ntb.NavigationTabBar;

/**
 * Created by admin on 2018/4/13.
 */

public class BlockNavigationTabBar extends NavigationTabBar {


    private OnTabClickListener mTabClickListener;


    public BlockNavigationTabBar(Context context) {
        super(context);
    }

    public BlockNavigationTabBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public BlockNavigationTabBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (this.mAnimator.isRunning()) {
            return true;
        } else if (this.mScrollState != 0) {
            return true;
        } else {
            switch (event.getAction()) {
                case 0:
                    this.mIsActionDown = true;
                    if (this.mIsViewPagerMode && this.mIsSwiped) {
                        if (this.mIsHorizontalOrientation) {
                            this.mIsPointerActionDown = (int) (event.getX() / this.mModelSize) == this.mIndex;
                        } else {
                            this.mIsPointerActionDown = (int) (event.getY() / this.mModelSize) == this.mIndex;
                        }
                    }
                    break;
                case 2:
                    if (this.mIsPointerActionDown) {
                        int index;
                        if (this.mIsHorizontalOrientation) {
                            index = (int) (event.getX() / this.mModelSize);
                        } else {
                            index = (int) (event.getY() / this.mModelSize);
                        }
                        if (mTabClickListener == null || (mTabClickListener != null && mTabClickListener.onPre(index))) {
                            this.mViewPager.setCurrentItem(index, true);
                        }
                        break;
                    } else if (this.mIsActionDown) {
                        break;
                    }
                case 1:
                    if (this.mIsActionDown) {
                        this.playSoundEffect(0);
                        if (this.mIsHorizontalOrientation) {
                            this.setModelIndex((int) (event.getX() / this.mModelSize));
                        } else {
                            this.setModelIndex((int) (event.getY() / this.mModelSize));
                        }
                    }
                case 3:
                case 4:
                default:
                    this.mIsPointerActionDown = false;
                    this.mIsActionDown = false;
            }

            return true;
        }
    }

    @Override
    public void setModelIndex(int index) {
        if (mTabClickListener == null || (mTabClickListener != null && mTabClickListener.onPre(index))) {
            super.setModelIndex(index);
        }
    }

    public void setTabClickListener(OnTabClickListener listener) {
        this.mTabClickListener = listener;
    }

    public static interface OnTabClickListener {

        boolean onPre(int position);
    }
}
